//
//  JAStatusListViewModel.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/9.
//  Copyright © 2017年 Arvin. All rights reserved.
//

import UIKit
import SDWebImage

class JAStatusListViewModel: UITableViewCell {
    
    /// 数据源数组
    var dataSourceArr: [JAStatusViewModel] = []
    
    
    // MARK: - 加载数据
    func loadData(isPullDown: Bool,callBack: @escaping (Bool)->()) {
        
        var since_id: Int64 = 0
        var max_id: Int64 = 0
        
//        var isPullDown = true
        
        //正在下拉刷新,since_id要传值 , max_id传0
        if isPullDown {
            since_id = dataSourceArr.first?.statusModel.id ?? 0
        } else {
            max_id = dataSourceArr.last?.statusModel.id ?? 0
        }
        
        //调用requestStatus获取微博的数据
        NetworkTool.shared.requestStatus(sinceId: since_id, maxId: max_id)  { (responseObject) in
            
            //将可选的Any类型(responseObject)弱转在可选的[WBStatusModel], 再做可选绑定
            if let statusModelArr = responseObject as? [JAStatusModel] {
                
                //将model数组转成viewModel数组
                var viewModelArray: [JAStatusViewModel] = []
                
                for model in statusModelArr {
                    let viewModel = JAStatusViewModel(statusModel: model)
                    viewModelArray.append(viewModel)
                }
                //数据拼接: 如果是下拉, 将获取的数据塞到数据源的前面; 如果是下拉, 将获取的数据拼在数据源的后面
                if isPullDown == true {
                    
                    self.dataSourceArr = viewModelArray + self.dataSourceArr
                    
                } else {
                    
                    //上拉刷新时, 有一条重复的数据, 需要将其删除
                    //statusModelArr.removeFirst()
                    //如果返回数据大于一条,则将第一条数据删除,避免数据重复
                    if viewModelArray.count > 0{
                        
                        viewModelArray.removeFirst()
                    }
                    
                    self.dataSourceArr += viewModelArray
                }
                
                //在reload TableView之前, 先下载所有的单张图片
                self.dealWithSinglePicture(viewModelArray: viewModelArray, callBack: callBack)
            } else {
                callBack(false)
            }
        }
    }
    
    // MARK: - 处理所有的单张图片
    func dealWithSinglePicture (viewModelArray: [JAStatusViewModel], callBack: @escaping (Bool)->()) {
        //创建一个调度组
        let group = DispatchGroup()
        
        //遍历viewModelAyy, 找到单张图片
        for viewModel in viewModelArray {
            //判断是否是单张图片
            if let pic_urls = viewModel.pic_urls,pic_urls.count == 1 {
                group.enter()
                let urlStr = pic_urls[0].thumbnail_pic
                let url = URL(string: urlStr!)
                SDWebImageManager.shared().loadImage(with: url, options: [], progress: nil, completed: { (singleImage, _, _, _, _, _) in
                    //计算单张图片的size,并更新viewModel
                    if let singleImage = singleImage {
                        
                        var imageSize = singleImage.size
                        
                        //如果图片过宽
                        let newWidth = screenWidh - 60
                        if imageSize.width > screenWidh - 20 {
                            //原高/原宽 = 新高/新宽
                            imageSize.height = imageSize.height * newWidth / imageSize.width
                            imageSize.width = newWidth
                        }
                        
                        viewModel.pictureViewSize = imageSize
                        
                        group.leave()
                        
                    }
                })
            }
            
        }
        
        group.notify(queue: DispatchQueue.main) {
            callBack(true)
        }
    }
}
